package exam.freemarker.directive;

import exam.freemarker.config.FreemarkerComponent;
import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/17 12:22
 * @since JDK 1.8
 */
@FreemarkerComponent("Tag")
public class FreeMarrkerDirective implements TemplateDirectiveModel {

    private static final String[] LIST={"test1","test2","test3"};

    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {

        Map<String, Object> objectMap=new HashMap<>();
        objectMap.put("bean2",LIST);
        DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
        environment.setVariable("bean", builder.build().wrap(objectMap));
        templateDirectiveBody.render(environment.getOut());
    }
}
